
<template>
  <div class="page-container">
    <ex_super_table_page v-bind="option" ref="SuperTablePage">
    </ex_super_table_page>

    <ex_super_dialog_form ref="accountDialog" v-bind="accountDialog" @afterSubmitSuccess="reload()">
    </ex_super_dialog_form>

    <ex_super_dialog_form ref="creditDialog" v-bind="creditDialog" @afterSubmitSuccess="reload()">
    </ex_super_dialog_form>

  </div>
</template>
<script>
import business_function from "@/common/business_function";
import FmsCurrencyChoose from '@/component/fms/fms-currency-choose.vue'

import superTablePageFunction from '@/mixin/super-table-page-function';
export default {

  title: "title_account_manage",
  components: { FmsCurrencyChoose, },

  mixins: [superTablePageFunction],
  data() {



    return {

      option: {
        search: {
          model: [
            {
              label: "label_fms_account_cmp_customer", key: "customerIds", type: 'common_customer_choose', attrs: { multiple: true, }, inputStyle: { 'width': '300px' },
            },
            {
              label: "label_account_type", key: "accountType", type: 'select', enum: 'CustomerAccountTypeEnum', attrs: { clearable: true },
            },
            {
              label: "label_currency_kind", key: "currencyType", type: 'select', enum: 'CurrencyTypeEnum', attrs: { clearable: true },
            },
            {
              label: "label_fms_account_cmp_is_product", key: "productType", type: 'select', enum: 'PmsProductTypeEnum', attrs: { clearable: true },
            },
            {
              label: "label_status", key: "status", type: 'select', enum: 'IsAvailableStatusEnum', attrs: { clearable: true },
            },
            {
              label: ['label_fms_account_cmp_usable', 'label_money'], key: "startAmount"
            },
            {
              label: "~", key: "endAmount"
            },
          ],
        },
        action: [
          {
            label: 'label_fms_account_cmp_add_account',
            auth: 'fms_account_list_edit',
            attrs: { icon: "el-icon-plus" }, click: () => {
              this.$refs.accountDialog.showNew({
                title: this.$t('label_fms_account_cmp_add_account'),
                data: {},
                onlySetData: true,
              })
            }
          },
          {
            label: 'label_export',
            click: this.exportExcel
          },
        ],

        table: {
          url: "/fms-api/customer/CustomerAccountRpc/query.json",
          attrs: {
            'cell-style': ({ row, column, rowIndex, columnIndex }) => {
              if (column.property == 'status') {
                return { color: row.status == 'NORMAL' ? this.$globalCss.successColor : this.$globalCss.infoColor }
              }
            }
          },
          model: [
            {
              key: 'companyName', label: 'label_fms_account_cmp_customer_name', align: 'left', attrs: {
                formatter: (r, c, v, i) => {
                  return '[' + r.customerCode + '] ' + r.customerName
                }
              }
            },

            {
              key: 'accountType', label: 'label_fms_account_log_account', width: 200,
              attrs: {
                formatter: (r, c, v, i) => {
                  return (r.accountType == 'CREDIT' ? (this.$enumShow(r.productType, 'PmsProductTypeEnum') + '-') : '') + this.$enumShow(r.currencyType, 'CurrencyTypeEnum') + '-' + this.$enumShow(r.accountType, 'CustomerAccountTypeEnum')
                }
              }
            },

            { key: 'creditAmount', label: ['label_fms_account_cmp_credit_name', 'label_money'], align: 'right', if() { return this.accountType == 'CREDIT' }, },
            { key: 'lockAmount', label: ['label_fms_account_cmp_withholding', 'label_money'], align: 'right' },
            { key: 'freezeAmount', label: ['label_fms_account_cmp_freeze', 'label_money'], align: 'right' },
            { key: 'leftAmount', label: ['label_fms_account_cmp_usable', 'label_money'], align: 'right' },
            { key: 'totalAmount', label: 'label_fms_account_cmp_account_balance', align: 'right', if: () => { return this.accountType == 'CASH' } },
            { key: 'creditAmount', label: 'label_fms_account_cmp_line_of_credit', align: 'right', if: () => { return this.accountType == 'CREDIT' } },
            {
              key: 'status', label: 'label_fms_account_cmp_account_status', width: 120,
              render:(h,s)=>this.$renderText(h,s,{enum:"IsAvailableStatusEnum"})
            },
            {
              type: 'operate', width: 180,
              operate: [

                {
                  label: 'IsAvailableStatusEnum__DISABLED',
                  auth: 'fms_account_list_disable',
                  buttonClass: "button_text_danger", confirmButtonType: 'danger',

                  type: "confirm-button",
                  message: this.$tt("label_fms_account_cmp_freeze_user_confirm"),
                  url: '/fms-api/customer/CustomerAccountRpc/disable.json',
                  if: (row) => {
                    return row.status == 'ENABLE'
                  },
                  params: (v) => {
                    return { id: v.id }
                  },
                  done: ['reload', 'tip'],
                },
                {
                  label: 'IsAvailableStatusEnum__ENABLE',
                  auth: 'fms_account_list_disable',
                  buttonClass: "button_text_success", confirmButtonType: 'success',

                  type: "confirm-button",
                  message: "label_fms_account_cmp_unfreeze_user_confirm",
                  url: '/fms-api/customer/CustomerAccountRpc/enable.json',
                  if: (row) => {
                    return row.status != 'ENABLE'
                  },
                  params: (v) => {
                    return { id: v.id }
                  },
                  done: ['reload', 'tip']
                },
                {
                  label: 'label_fms_account_cmp_adjust_credit',
                  auth: 'fms_account_list_adjust_credit',

                  if: (row) => {
                    return row.accountType == 'CREDIT'
                  },
                  click: (row) => {
                    this.$refs.creditDialog.showNew({
                      title: this.$t('label_fms_account_cmp_credit_apply'),
                      params: { id: row.id },
                    })
                    // this.$ajax({
                    //   url: '/fms-api/customer/CustomerAccountRpc/detail.json',
                    //   data: { id: row.id },
                    //   success: (res) => {
                    //     this.creditForm = JSON.parse(JSON.stringify(res.data))
                    //     this.creditDialogFormVisible = true
                    //   }
                    // });
                  }
                }
              ],
            }
          ]
        }
      },

      accountDialog: {
        customClass: 'dialog_auto',
        submitUrl: '/fms-api/customer/CustomerAccountRpc/batchSave.json',
        submitDataHandle: (data) => {
          if (data.accountType == 'CREDIT') {
            return {
              accountList: [data]
            }
          }
          let { currencyType, currencyTypes, ...item } = data
          if (currencyType) {
            return {
              accountList: [Object.assign({ currencyType: currencyType }, item)]
            }
          }
          return {
            accountList: currencyTypes.map(it => {
              return Object.assign({ currencyType: it }, item)
            })
          }
        },
        form: {
          itemClass: "form_item_default", inputClass: "form_input_default",
          model: [
            { key: 'customerId', label: 'label_fms_account_cmp_as_customer', type: "common_customer_choose" },
            { key: 'accountType', label: 'label_account_type', type: "select", enum: "CustomerAccountTypeEnum" },
            {
              key: 'currencyTypes', label: 'label_fms_account_cmp_account_currency', if: (data) => this.$loginUser.ifElse澳越 && data.accountType == 'CASH',
              type: FmsCurrencyChoose, multiple: true,
            },
            {
              key: 'currencyType', label: 'label_fms_account_cmp_account_currency', if: (data) => !this.$loginUser.ifElse澳越 && data.accountType == 'CASH',
              type: FmsCurrencyChoose,
            },
            {
              key: 'productType', label: 'label_fms_account_cmp_is_product', if: (data) => data.accountType == 'CREDIT',
              type: "select", enum: "PmsProductTypeEnum",
              listHandler: list => list.filter(it => it != 'INSURANCE' && it != 'SWAP_TRANSPORT'),
            },
            {
              key: 'creditAmount', label: 'label_fms_account_cmp_credit', if: (data) => data.accountType == 'CREDIT',
            },
          ],
          rules: {
            'customerId': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'accountType': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'currencyType': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],

            'currencyTypes': [{ required: true, type: 'array', message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'productType': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'creditAmount': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
          },
        }
      },
      creditDialog: {
        customClass: 'dialog_auto',
        submitUrl: '/fms-api/customer/CustomerCreditApplyRpc/apply.json',
        detailUrl: '/fms-api/customer/CustomerAccountRpc/detail.json',
        submitSureText: 'label_fms_account_cmp_submit_apply',
        form: {
          itemClass: "form_item_default", inputClass: "form_input_default",
          model: [
            {
              key: 'customerId', label: 'label_fms_account_cmp_as_customer',
              render: (h, { data, item }) => this.$renderText(h, this.$formatNameCode(data.customerName, data.customerCode))
            },
            {
              key: 'productType', label: 'label_fms_account_cmp_is_product',
              render: (h, { data, item }) => this.$renderText(h, this.$enumShow(data[item.key], 'PmsProductTypeEnum'))
            },
            {
              key: 'currencyType', label: 'label_fms_account_cmp_account_currency',
              render: (h, { data, item }) => this.$renderText(h, this.$enumShow(data[item.key], 'CurrencyTypeEnum'))
            },
            { key: 'creditAmount', label: 'label_fms_account_cmp_current_credit', render: (h, { data, item }) => this.$renderText(h, data[item.key]) },
            {
              key: 'newCreditAmount', label: 'label_fms_account_cmp_adjust_as',
            },
            {
              key: 'reason', label: 'label_fms_account_cmp_adjust_reason',
            },
          ],
          rules: {
            'newCreditAmount': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
            'reason': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
          },
        }
      },
    };
  },




  methods: {

    exportExcel() {
      business_function.confirmOperateBaseSelectOrForm.call(this, {
        data: this.$refs.SuperTablePage.getSearchFormData(),
        model: this.option.search.model,
        selection: this.selection,
        callback: (selection, data) => {
          let params = {}
          if (!_.isEmpty(selection)) {
            params = {
              idList: selection.map(it => it.id)
            }
          } else {
            params = data
          }
          this.$functionCommon({
            url: '/fms-api/customer/CustomerAccountRpc/export.json',
            data: params
          })
        }
      })
    },

  }
};

</script>
<style></style>
